package com.foxcai.novel.action;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

import org.dom4j.Document;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;

import com.foxcai.novel.base.DBConnection;
import com.foxcai.novel.html.JspContextServices;
import com.foxcai.novel.http.EasouHttpCommAction;

public class GetNovelDetailToDb {

	public static Boolean InsertToDb(String xml) {

		JspContextServices utb = new JspContextServices();

		Connection conn = null;

		PreparedStatement pstm = null;
		Document doc = null;
		try {
			doc = DocumentHelper.parseText(xml);
			Element root = doc.getRootElement();
			String NovId = root.elementText("novelid");
System.out.println("novelid:"+NovId);
			if (!NovId.equals("")) {
				conn = DBConnection.newInstance().getConnection();
				String sql = "insert into NovelDetails (NovId,NovName,ChapName,sort,sortcount,category,author,NovCont,SourUrl,UpdTime) values (?,?,?,?,?,?,?,?,?,?)";

				pstm = conn.prepareStatement(sql);

				pstm.setInt(1, Integer.parseInt(root.elementText("novelid")));
				pstm.setString(2, root.elementTextTrim("novelname"));
				pstm.setString(3, root.elementTextTrim("chaptername"));
				pstm.setInt(4, Integer.parseInt(root.elementText("sort")));
				pstm.setInt(5, Integer.parseInt(root.elementText("sortcount")));
				pstm.setString(6, root.element("category").elementText("cn"));
				pstm.setString(7, root.elementTextTrim("author"));
				pstm.setString(8, root.elementText("content"));
				pstm.setString(9, root.elementText("url"));
				pstm.setString(10, utb.getServerTime("yyyy-MM-dd HH:mm:ss"));

				pstm.execute();
				System.out.println("insert success");

			}

		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			doc.clearContent();
			try {
				if (pstm != null) {
					pstm.close();
				}
				if (conn != null) {
					conn.close();
				}
			} catch (Exception ex) {
				ex.printStackTrace();
			}
		}

		return null;

	}

	/**
	 * @param args
	 */
	public static void main(String[] args) {

		DBConnection conn = null;

		ResultSet rs = null;
		String reqUrl = "http://b1.easou.com/getChapterInfoImpl.m";
		String reqParams = null;
		String resultXml = null;
		EasouHttpCommAction ehca = new EasouHttpCommAction();
		long beg = System.currentTimeMillis();

		try {
			conn = DBConnection.newInstance();
			String sql = "select NovId from NovelList order by NovAcc desc";

			rs = conn.executeQuery(sql);
			while (rs.next()) {
				reqParams = "id="+rs.getInt("NovId")+"";
System.out.println("reqParams:"+reqParams);
				resultXml = ehca.send(reqParams, reqUrl);
				Thread.sleep(1000);   
				InsertToDb(resultXml);
			}

		} catch (SQLException e) {
			e.printStackTrace();
		}catch (InterruptedException e) {
			e.printStackTrace();
		}  

		long curTime = System.currentTimeMillis();
		long intvl = curTime - beg;
		System.out.println("10条数据耗时：" + intvl + "毫秒！！");

	}
}
